"use strict";

require("../__util__/test-init");

var fs = require("fs");
var { JSDOM } = require("jsdom");
var vdom = require("marko/runtime/vdom/vdom");
var autotest = require("mocha-autotest").default;
var domToString = require("../__util__/domToString");

var document = new JSDOM().window.document;
var VElement = vdom.___VElement;
var VText = vdom.___VText;
var VDocumentFragment = vdom.___VDocumentFragment;

var vdomHelpers = {
  createElement: function (
    tagName,
    attrs,
    key,
    component,
    childCount,
    flags,
    props,
  ) {
    return new VElement(
      tagName,
      attrs,
      key,
      component,
      childCount,
      flags,
      props,
    );
  },
  createText: function (value) {
    return new VText(value);
  },
  createDocumentFragment: function () {
    return new VDocumentFragment();
  },
  VElement: VElement,
  virtualizeElement: VElement.___virtualize,
};

autotest("fixtures", (fixture) => {
  let test = fixture.test;
  let resolve = fixture.resolve;
  let snapshot = fixture.snapshot;
  test(() => {
    var helpers = {};
    helpers.vdom = vdomHelpers;
    helpers.document = document;

    var mainPath = resolve("index.js");
    if (fs.existsSync(mainPath)) {
      var main = require(mainPath);
      var rootNode = main(helpers);

      var rootNodeHTML = rootNode != null ? domToString(rootNode) : "(null)";
      snapshot(rootNodeHTML, ".html");
    }
  });
});
